import TimeShow from '../../utils/TimeShow.js'
Page({
  data: {
    userLie: [],
    searchUserLie: [],
    searchMsg: '',
    isShow: false,
    // 开始移动时的位置
    startX: 0,
    startY: 0,
    tonzhi: {}
  },
  timer: null,
  move: false,
  id: null,
  instance: null,

  // 下拉刷新
  onPullDownRefresh() {
    this.onShow()
  },
  onReady() {
    // this.onMes()
  },

  onMessage(data) {
    this.setNew()
    if (getApp().globalData.wdxx > 0) {
      wx.setTabBarBadge({
        index: 3, //从左边开始第几个页面显示
        text: '' + getApp().globalData.wdxx //消息条数
      })
    } else {
      wx.removeTabBarBadge({
        index: 3
      })
    }
  },

  onLoad: function (options) {},
  onHide() {
    // wx.setStorageSync('setShow', false)
    // this.onMes()
  },
  onShow() {
    this.checkTong()

    this.setNew()
    wx.stopPullDownRefresh()

    // 设置未读消息条数
    if (getApp().globalData.wdxx > 0) {
      wx.setTabBarBadge({
        index: 3, //从左边开始第几个页面显示
        text: '' + getApp().globalData.wdxx //消息条数
      })
    } else {
      wx.removeTabBarBadge({
        index: 3
      })
    }
  },

  handClick(e) {
    if (this.instance) {
      this.instance.close()
      this.move = false
    }
  },
  handleOpen(e) {
    const { id } = e.currentTarget
    this.instance = this.selectComponent(`#${id}`)
    this.move = true
    console.log('open:',e)
    if (id == this.id) {}
  },

  userXian(e) {
    if (this.move) return
    let userLie = this.data.userLie.map((item) => {
      if (item.isTouchMove) {
        return {
          ...item,
          isTouchMove: false
        }
      } else {
        return item
      }
    })
    if (this.noNav) {
      this.setData({
        userLie
      })
    } else {
      const { chatid } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/demes/demes?chatId=${chatid}`
      })
    }
  },

  // 用户回复的消息
  handMes() {
    wx.navigateTo({
      url: '/pages/huifu/huifu'
    })
  },
  // 收到的赞
  handZan() {
    wx.navigateTo({
      url: '/pages/souzhan/souzhan'
    })
  },
  // 收到的礼物
  handLi() {
    wx.navigateTo({
      url: '/pages/liwu/liwu'
    })
  },
  // 系统通知
  handTong() {
    wx.navigateTo({
      url: '/pages/xitong/xitong'
    })
  },

  // 滑动扩展功能
  // touchStart(e) {
  //   let userLie = this.data.userLie
  //   userLie.forEach((item) => {
  //     if (item.isTouchMove) {
  //       item.isTouchMove = false
  //     }
  //   })
  //   this.setData({
  //     userLie,
  //     startX: e.changedTouches[0].clientX,
  //     startY: e.changedTouches[0].clientY
  //   })
  // },
  // touchMove(e) {
  //   const { chatid } = e.currentTarget.dataset
  //   const startX = this.data.startX
  //   const startY = this.data.startY
  //   // 滑动后坐标
  //   let moveX = e.changedTouches[0].clientX
  //   let moveY = e.changedTouches[0].clientY
  //   if (moveX - startX > -5) return
  //   // 滑动角度
  //   let angle = this.angle({ X: startX, Y: startY }, { X: moveX, Y: moveY })
  //   let userLie = this.data.userLie
  //   userLie.some((item) => {
  //     item.isTouchMove = false
  //     if (Math.abs(angle) > 30) return true
  //     if (item.chatId === chatid) {
  //       if (moveX > startX) {
  //         item.isTouchMove = false
  //       } else {
  //         item.isTouchMove = true
  //         this.noNav = true
  //       }
  //       return true
  //     }
  //   })
  //   this.setData({
  //     userLie: [...userLie]
  //   }, () => {
  //     console.log(this.data.userLie)
  //   })
  // },
  // 计算滑动角度
  // @param {Object} start 起点坐标
  // @param {Object} end 终点坐标
  // angle(start, end) {
  //   var _X = end.X - start.X
  //   var _Y = end.Y - start.Y
  //   return (360 * Math.atan(_Y / _X)) / (2 * Math.PI)
  // },

  // 删除操作
  delete(e) {
    const { index, chatid } = e.currentTarget.dataset
    const userLie = this.data.userLie
    let message = wx.getStorageSync('message')
    userLie.splice(index, 1)
    this.setData(
      {
        userLie
      },
      () => {
        delete message[chatid]
        wx.setStorageSync('userLie', userLie)
        wx.setStorageSync('message', message)
      }
    )
  },

  // 置顶操作
  handTop(e) {
    const { chatid } = e.currentTarget.dataset
    let { userLie } = this.data
    userLie.some((item) => {
      if (item.chatId === chatid) {
        item.isTop = true
        return true
      }
    })
    this.setData(
      {
        userLie: this.setUserline(userLie)
      },
      () => {
        wx.setStorageSync('userLie', this.data.userLie)
      }
    )
  },
  // 取消置顶
  handTopF(e) {
    const { chatid } = e.currentTarget.dataset
    let { userLie } = this.data
    userLie.some((item) => {
      if (item.chatId === chatid) {
        item.isTop = false
        return true
      }
    })
    this.setData(
      {
        userLie: this.setUserline(userLie)
      },
      () => {
        wx.setStorageSync('userLie', this.data.userLie)
      }
    )
  },
  // 时间及未读消息
  maoPAo(array) {
    let temp = null
    let time1 = null
    let time2 = null
    for (let i = 1; i <= array.length - 1; i++) {
      for (let j = 0; j <= array.length - i - 1; j++) {
        time1 = +new Date(array[j].lasttime)
        time2 = +new Date(array[j + 1].lasttime)
        if (time2 > time1) {
          temp = array[j + 1]
          array[j + 1] = array[j]
          array[j] = temp
        }
      }
    }
  },
  setUserline(array) {
    let topArray = []
    let otherArray = []
    for (let i = 0; i <= array.length - 1; i++) {
      if (array[i].isTop) {
        topArray.push(array[i])
      } else {
        otherArray.push(array[i])
      }
    }
    this.maoPAo(topArray)
    this.maoPAo(otherArray)
    const newArray = topArray.concat(otherArray)
    return newArray
  },

  // 刷新
  setNew() {
    let userLie = wx.getStorageSync('userLie')
    if (userLie.length <= 0) return
    userLie.forEach((item) => {
      item['time'] = TimeShow(item['lasttime'])
    })
    userLie = this.setUserline(userLie)
    wx.setStorageSync('userLie', userLie)
    this.setData({
      userLie
    })
  },

  // 检查是否有未读通知
  checkTong() {
    let { tonzhi } = this.data
    let pl = wx.getStorageSync('pinglun')
    let dianzang = wx.getStorageSync('dianzang')
    let sonli = wx.getStorageSync('sonli')
    let xitong = wx.getStorageSync('xitong')
    if (pl.length > 0) {
      let isBiao = true
      pl.some((item) => {
        if (item['isweidu']) {
          tonzhi['pl'] = true
          isBiao = false
          return true
        }
      })
      if (isBiao) tonzhi['pl'] = false
    }
    if (dianzang.length > 0) {
      let isBiao = true
      dianzang.some((item) => {
        if (item['isweidu']) {
          tonzhi['dianzang'] = true
          isBiao = false
          return true
        }
      })
      if (isBiao) tonzhi['dianzang'] = false
    }
    if (sonli.length > 0) {
      let isBiao = true
      sonli.some((item) => {
        if (item['isweidu']) {
          tonzhi['sonli'] = true
          isBiao = false
          return true
        }
      })
      if (isBiao) tonzhi['sonli'] = false
    }
    if (xitong.length > 0) {
      let isBiao = true
      xitong.some((item) => {
        if (item['isweidu']) {
          tonzhi['xitong'] = true
          isBiao = false
          return true
        }
      })
      if (isBiao) tonzhi['xitong'] = false
    }

    this.setData({
      tonzhi
    })
  },

  // 用户搜索 防抖
  changInput(e) {
    const searchMsg = e.detail.value.trim()
    if (!searchMsg) return
    this.setData({
      searchMsg: searchMsg,
      isShow: true
    })
    clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      const searchUserLie = this.data.userLie.map((item) => {
        if (item.name.includes(searchMsg)) {
          return item
        }
      })
      if (!searchUserLie[0]) {
        searchUserLie.length = 0
      }
      this.setData({
        searchUserLie
      })
    }, 800)
  },

  setIsShow() {
    this.setData({
      isShow: false,
      searchMsg: ''
    })
  }
})
